package system

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	useragent "github.com/wenlng/go-user-agent"
	"github.com/xuri/excelize/v2"
	"net/http"
	"ruoyi-go/app/admin/model/constants"
	"ruoyi-go/app/admin/model/monitor"
	"ruoyi-go/app/admin/model/system"
	"ruoyi-go/app/admin/model/tools"
	"ruoyi-go/app/admin/service"
	"ruoyi-go/config"
	"ruoyi-go/pkg/cache/redisCache"
	"ruoyi-go/pkg/mysql"
	"ruoyi-go/utils"
	"ruoyi-go/utils/R"
	"ruoyi-go/utils/jwt"
	"ruoyi-go/utils/password"
	"strconv"
	"strings"
	"time"
)

func LoginHandler(context *gin.Context) {
	var param system.LoginParam
	if err := context.ShouldBind(&param); err != nil {
		service.SysLoginFor.LoginInfoAdd(context, param, "登录失败，参数为空", false)
		panic("参数不能为空")
		return
	}
	if !loginBeforeCheck(param, context) {
		return
	}
	isExist := service.SysUserService.IsExistUser(param.UserName)
	if !isExist {
		service.SysLoginFor.LoginInfoAdd(context, param, "登录失败，用户不存在", false)
		panic("用户不存在")
		return
	}
	findUser(param, context)
}

func loginBeforeCheck(param system.LoginParam, context *gin.Context) bool {
	if param.Code == "" || param.Uuid == "" {
		service.SysLoginFor.LoginInfoAdd(context, param, "登录失败，验证码不能为空", false)
		panic("验证码不能为空")
	}
	// 验证码验证
	var captchaEnabled = SelectCaptchaEnabled()
	if !captchaEnabled {
		return false
	}
	isVerify := utils.VerifyCaptcha(param.Uuid, param.Code)
	service.SysLoginFor.LoginInfoAdd(context, param, "登录失败，验证码错误", false)
	if !isVerify {
		panic("验证码错误")
	}
	return isVerify
}

// 判断用户是否存在 返回bool类型
func findUser(param system.LoginParam, context *gin.Context) {
	var loginName = param.UserName
	var pass = param.Password
	var user = service.SysUserService.FindUserByName(loginName)
	if user.UserId <= 0 {
		service.SysLoginFor.LoginInfoAdd(context, param, "登录失败，用户不存在", false)
		panic("用户不存在")
		return
	}
	if user.Status == "1" {
		service.SysLoginFor.LoginInfoAdd(context, param, "登录失败，账号已停用", false)
		panic("账号已停用")
		return
	}
	// 验证密码错误次数
	count := password.GetPasswordTryCount(param.UserName)
	if count >= config.UserPassword.MaxRetryCount {
		service.SysLoginFor.LoginInfoAdd(context, param, "登录失败，用户密码错误最大次数", false)
		panic("登录失败，用户密码错误最大次数，请过稍后再试")
		return
	}
	// 验证 密码是否正确
	if !utils.PasswordVerify(pass, user.Password) {
		count++
		password.SetPasswordTryCount(param.UserName, count)
		service.SysLoginFor.LoginInfoAdd(context, param, "登录失败，密码错误", false)
		panic("登录失败，密码错误")
		return
	} else {
		password.DeletePasswordTryCount(param.UserName)
	}
	newUUID, _ := uuid.NewUUID()
	tokenId := newUUID.String()
	dept := service.SysDeptService.GetDeptInfo(strconv.Itoa(user.DeptId))
	userAgent := context.Request.Header.Get("User-Agent")
	os := useragent.GetOsName(userAgent)
	browser := useragent.GetBrowserName(userAgent)
	tokenString, err := jwt.CreateToken(user.UserName, user.UserId, user.DeptId, newUUID.String())
	if err != nil {
		service.SysLoginFor.LoginInfoAdd(context, param, "登录失败，"+err.Error(), false)
		panic("登录失败")
		return
	}

	// 获取用户角色
	roleIds := service.SysUserRoleService.SelectRoleIdsByUserId(user.UserId)
	var roleKey = make([]string, len(roleIds))
	var permissionCode = make([]string, len(roleIds))
	if len(roleIds) > 0 {
		roleKey = service.SysRoleService.SelectRoleKeyByRoleIds(roleIds)
		// 查询角色关联权限
		menuIds := service.SysRoleMenuService.SelectMenuIdByRoleId(roleIds)
		if len(menuIds) > 0 {
			perms := service.SysMenuService.SelectPermsCodeListByMenuIds(menuIds)
			permissionCode = append(permissionCode, perms...)
		}
	}

	//写入缓存
	loginUser, _ := json.Marshal(&monitor.LoginUserCache{
		TokenId:        tokenId,
		DeptName:       dept.DeptName,
		Ipaddr:         "" + context.ClientIP(),
		LoginLocation:  "" + utils.GetRealAddressByIP(context.ClientIP()),
		UserId:         user.UserId,
		Browser:        browser,
		Os:             os,
		LoginTime:      time.Now().Unix(),
		UserName:       user.UserName,
		Uuid:           tokenId,
		DeptId:         user.DeptId,
		RoleKey:        roleKey,
		PermissionCode: permissionCode,
	})
	err = redisCache.RedisDB.Put(constants.LoginCacheKey+newUUID.String(), string(loginUser), time.Duration(config.Jwt.JwtTtl)*time.Second)
	if err != nil {
		service.SysLoginFor.LoginInfoAdd(context, param, "登录失败，"+err.Error(), false)
		panic("登录失败")
		return
	}
	// 登录日志
	service.SysLoginFor.LoginInfoAdd(context, param, "登录成功", true)
	R.SuccessData(context, tokenString)
}

func GetInfoHandler(context *gin.Context) {
	userId, _ := context.Get("userId")
	var user = service.SysUserService.FindUserById(userId)
	roles := service.SysRoleService.GetRolePermission(user)
	permissions := service.SysRoleService.GetMenuPermission(user)
	dept := service.SysDeptService.GetDeptInfo(strconv.Itoa(user.DeptId))
	loginUserInfo := map[string]any{
		"user": map[string]any{
			"userName":    user.UserName,
			"nickName":    user.NickName,
			"phonenumber": user.Phonenumber,
			"email":       user.Email,
			"avatar":      user.Avatar,
			"sex":         user.Sex,
			"createTime":  user.CreateTime,
			"dept":        dept,
		},
		"roles":       roles,
		"permissions": permissions,
	}
	R.SuccessData(context, loginUserInfo)
}

func LogoutHandler(context *gin.Context) {
	// 开始删除缓存
	jwtUuid, err := jwt.GetJwtUuid(context)
	if err != nil || len(jwtUuid) <= 0 {
		R.SuccessMsg(context, "退出成功")
		return
	}
	redisCache.RedisDB.Del(constants.LoginCacheKey + jwtUuid)
	R.SuccessMsg(context, "退出成功")
}

// CaptchaImageHandler 验证码 输出
func CaptchaImageHandler(context *gin.Context) {
	var captchaEnabled = SelectCaptchaEnabled()
	if captchaEnabled {
		id, b64s, _, err := utils.CreateImageCaptcha()
		if err != nil {
			panic("创建验证码失败，请联系管理员")
			return
		}
		res := map[string]any{
			"img":            strings.ReplaceAll(b64s, "data:image/png;base64,", ""),
			"captchaEnabled": captchaEnabled,
			"uuid":           id,
		}
		R.SuccessData(context, res)
	} else {
		R.SuccessData(context, captchaEnabled)
	}
}

// UpdatePwdHandler 修改密码
func UpdatePwdHandler(context *gin.Context) {
	userId, _ := context.Get("userId")
	var Password = service.SysUserService.FindUserPassWordById(userId)
	// 没有这个，下面的为空很奇怪
	context.DefaultPostForm("newPassword", "")
	from := context.Request.Form
	OldPassword := from.Get("oldPassword")
	NewPassword := from.Get("newPassword")

	if OldPassword == "" {
		panic("参数不能为空")
		return
	}

	if NewPassword == "" {
		panic("参数不能为空")
		return
	}

	// 验证旧密码
	if utils.PasswordVerify(OldPassword, Password) {
		// 验证新密码
		if utils.PasswordVerify(NewPassword, Password) {
			panic("新密码不能与旧密码相同")
			return
		}
		// 加密
		passString, err3 := utils.PasswordHash(NewPassword)
		if err3 != nil {
			panic("加密失败")
			return
		}
		// 更新 密码
		service.SysUserService.UploadUserPassword(userId, passString)
		panic("修改密码成功")
	} else {
		panic("旧密码错误")
		return
	}
}

// ProfileHandler 查询个人信息
func ProfileHandler(context *gin.Context) {
	userId, _ := context.Get("userId")
	var user system.SysUser
	err1 := mysql.MysqlDb().Where("user_id = ?", userId).Preload("Dept").First(&user)

	if err1.Error != nil {
		panic("未找到用户")
		return
	}
	user.Password = ""
	res := map[string]any{
		"user": map[string]any{
			"userName":    user.UserName,
			"nickName":    user.NickName,
			"phonenumber": user.Phonenumber,
			"email":       user.Email,
			"sex":         user.Sex,
			"dept":        user.Dept,
			"createTime":  user.CreateTime.Format(constants.TimeFormat),
		},
		"roleGroup": service.SysRoleService.SelectRolesByUserName(user.UserName), // 目前暂时没有用
		"postGroup": service.SysPostsService.SelectUserPostGroup(user.UserName),  // 目前暂时没有用
	}
	R.SuccessData(context, res)
}

// PostProfileHandler 修改个人信息
func PostProfileHandler(context *gin.Context) {
	userId, _ := context.Get("userId")
	param := system.Userparam{}
	if err := context.ShouldBindJSON(&param); err != nil {
		panic("参数不能为空")
	}
	service.SysUserService.EditProfileUserInfo(utils.GetInterfaceToInt(userId), param)
	R.Success(context)
}

// AvatarHandler 上传头像 并更新
func AvatarHandler(context *gin.Context) {
	userId, _ := context.Get("userId")
	var user system.SysUser
	err1 := mysql.MysqlDb().Where("user_id = ?", userId).First(&user)

	if err1.Error != nil {
		panic("未找到用户")
		return
	}

	file, errLoad := context.FormFile("avatarfile")
	if errLoad != nil {
		msg := "获取上传文件错误:" + errLoad.Error()
		panic(msg)

		return
	}

	//fileExt := strings.ToLower(path.Ext(file.Filename))
	//if fileExt != ".png" && fileExt != ".jpg" && fileExt != ".gif" && fileExt != ".jpeg" {
	//	context.JSON(http.StatusOK, gin.H{
	//		"code": http.StatusInternalServerError,
	//		"msg":  "上传失败!只允许png,jpg,gif,jpeg文件",
	//	})
	//	return
	//}

	//上传图片
	errFile := context.SaveUploadedFile(file, "./static/images/"+file.Filename)
	if errFile != nil {
		panic("上传图片异常，请联系管理员")
		return
	}

	// 更新 头像
	err2 := mysql.MysqlDb().Model(&user).Update("avatar", "/profile/"+file.Filename)
	if err2.Error != nil {
		panic("上传图片异常，请联系管理员")
		return
	}
	R.SuccessData(context, "/profile/"+file.Filename)
}

/*-----------用户管理----------------------------*/

func ListUser(context *gin.Context) {
	pageNum, _ := strconv.Atoi(context.DefaultQuery("pageNum", "1"))
	pageSize, _ := strconv.Atoi(context.DefaultQuery("pageSize", "10"))

	deptId, _ := strconv.Atoi(context.DefaultQuery("deptId", "0"))
	var userName = context.DefaultQuery("userName", "")
	var status = context.DefaultQuery("status", "")

	var phonenumber = context.DefaultQuery("phonenumber", "")

	var beginTime = context.DefaultQuery("params[beginTime]", "")
	var endTime = context.DefaultQuery("params[endTime]", "")

	var param = tools.SearchTableDataParam{
		PageNum:  pageNum,
		PageSize: pageSize,
		Other: system.SysUser{
			DeptId:      deptId,
			UserName:    userName,
			Phonenumber: phonenumber,
			Status:      status,
		},
		Params: tools.Params{
			BeginTime: beginTime,
			EndTime:   endTime,
		},
	}
	list := service.SysUserService.SelectUserList(param, true)
	R.SuccessData(context, list)
}

func ExportExport(context *gin.Context) {
	pageNum, _ := strconv.Atoi(context.DefaultQuery("pageNum", "1"))
	pageSize, _ := strconv.Atoi(context.DefaultQuery("pageSize", "10"))

	deptId, _ := strconv.Atoi(context.DefaultQuery("deptId", "0"))
	var userName = context.DefaultQuery("userName", "")
	var status = context.DefaultQuery("status", "")

	var phonenumber = context.DefaultQuery("phonenumber", "")

	var beginTime = context.DefaultQuery("params[beginTime]", "")
	var endTime = context.DefaultQuery("params[endTime]", "")

	var param = tools.SearchTableDataParam{
		PageNum:  pageNum,
		PageSize: pageSize,
		Other: system.SysUser{
			DeptId:      deptId,
			UserName:    userName,
			Phonenumber: phonenumber,
			Status:      status,
		},
		Params: tools.Params{
			BeginTime: beginTime,
			EndTime:   endTime,
		},
	}
	var data1 = service.SysUserService.SelectUserParmList(param, true)
	var list = data1.Rows.([]system.SysUserExcel)

	//定义首行标题
	dataKey := make([]map[string]string, 0)
	dataKey = append(dataKey, map[string]string{
		"key":    "userId",
		"title":  "用户序号",
		"width":  "10",
		"is_num": "0",
	})
	dataKey = append(dataKey, map[string]string{
		"key":    "deptId",
		"title":  "部门编号",
		"width":  "15",
		"is_num": "0",
	})
	dataKey = append(dataKey, map[string]string{
		"key":    "userName",
		"title":  "登录名称",
		"width":  "20",
		"is_num": "0",
	})
	dataKey = append(dataKey, map[string]string{
		"key":    "nickName",
		"title":  "用户名称",
		"width":  "20",
		"is_num": "0",
	})
	dataKey = append(dataKey, map[string]string{
		"key":    "email",
		"title":  "用户邮箱",
		"width":  "20",
		"is_num": "0",
	})
	dataKey = append(dataKey, map[string]string{
		"key":    "phonenumber",
		"title":  "手机号码",
		"width":  "20",
		"is_num": "0",
	})
	dataKey = append(dataKey, map[string]string{
		"key":    "sex",
		"title":  "用户性别",
		"width":  "10",
		"is_num": "0",
	})
	dataKey = append(dataKey, map[string]string{
		"key":    "status",
		"title":  "帐号状态",
		"width":  "10",
		"is_num": "0",
	})
	dataKey = append(dataKey, map[string]string{
		"key":    "loginIp",
		"title":  "最后登录IP",
		"width":  "30",
		"is_num": "0",
	})
	dataKey = append(dataKey, map[string]string{
		"key":    "loginDate",
		"title":  "最后登录时间",
		"width":  "60",
		"is_num": "0",
	})
	dataKey = append(dataKey, map[string]string{
		"key":    "deptName",
		"title":  "部门名称",
		"width":  "50",
		"is_num": "0",
	})
	dataKey = append(dataKey, map[string]string{
		"key":    "leader",
		"title":  "部门负责人",
		"width":  "30",
		"is_num": "0",
	})

	//填充数据
	data := make([]map[string]interface{}, 0)
	if len(list) > 0 {
		for _, v := range list {
			var sexStatus = v.Sex
			var sex = ""
			if sexStatus == "0" {
				sex = "男"
			} else if sexStatus == "1" {
				sex = "女"
			} else {
				sex = "未知"
			}
			var status = v.Status
			var statusStr = ""
			if status == "0" {
				statusStr = "正常"
			} else {
				statusStr = "停用"
			}
			//timeObj, _ := time.Parse(time.RFC3339, v.LoginDate)
			var loginData = v.LoginDate.Format(constants.TimeFormat)
			data = append(data, map[string]interface{}{
				"userId":      v.UserId,
				"deptId":      v.DeptId,
				"userName":    v.UserName,
				"nickName":    v.NickName,
				"email":       v.Email,
				"phonenumber": v.Phonenumber,
				"sex":         sex,
				"status":      statusStr,
				"loginIp":     v.LoginIp,
				"loginDate":   loginData,
				"deptName":    v.DeptName,
				"leader":      v.Leader,
			})
		}
	}
	ex := tools.NewMyExcel()
	ex.ExportToWeb(dataKey, data, context)
}

func ImportUserData(context *gin.Context) {
	file, _, errLoad := context.Request.FormFile("file")
	if errLoad != nil {
		msg := "获取上传文件错误:" + errLoad.Error()
		panic(msg)
	}

	xlsx, err := excelize.OpenReader(file)
	if err != nil {
		panic("请选择文件")
	}
	var users []system.SysUserParm

	var updateSupport = context.DefaultQuery("updateSupport", "")

	rows, _ := xlsx.GetRows("Sheet1")
	for irow, row := range rows {
		if irow > 0 {
			var data []string
			for _, cell := range row {
				data = append(data, cell)
			}
			users = append(users, system.SysUserParm{
				UserName:    data[2],
				NickName:    data[3],
				Phonenumber: data[5],
				Sex:         data[6],
				Email:       data[4],
				Status:      data[7],
				CreateTime:  time.Now(),
				DeptId:      utils.GetInterfaceToInt(data[1]),
				PostIds:     utils.Split(data[8]),
				RoleIds:     utils.Split(data[9]),
			})
		}
	}
	if len(users) == 0 {
		panic("请在表格中添加数据")
	}

	var error, message = service.SysUserService.ImportUserData(users, updateSupport)

	if error == "" {
		context.JSON(http.StatusOK, R.ReturnSuccess(message))
	} else {
		panic(message)
	}

}

// 下载模版
func ImportTemplate(context *gin.Context) {
	//定义首行标题
	dataKey := make([]map[string]string, 0)
	dataKey = append(dataKey, map[string]string{
		"key":    "userId",
		"title":  "用户序号",
		"width":  "10",
		"is_num": "0",
	})
	dataKey = append(dataKey, map[string]string{
		"key":    "deptId",
		"title":  "部门编号",
		"width":  "15",
		"is_num": "0",
	})
	dataKey = append(dataKey, map[string]string{
		"key":    "userName",
		"title":  "登录名称",
		"width":  "20",
		"is_num": "0",
	})
	dataKey = append(dataKey, map[string]string{
		"key":    "nickName",
		"title":  "用户名称",
		"width":  "20",
		"is_num": "0",
	})
	dataKey = append(dataKey, map[string]string{
		"key":    "email",
		"title":  "用户邮箱",
		"width":  "20",
		"is_num": "0",
	})
	dataKey = append(dataKey, map[string]string{
		"key":    "phonenumber",
		"title":  "手机号码",
		"width":  "20",
		"is_num": "0",
	})
	dataKey = append(dataKey, map[string]string{
		"key":    "sex",
		"title":  "用户性别",
		"width":  "10",
		"is_num": "0",
	})
	dataKey = append(dataKey, map[string]string{
		"key":    "status",
		"title":  "帐号状态",
		"width":  "10",
		"is_num": "0",
	})
	dataKey = append(dataKey, map[string]string{
		"key":    "status",
		"title":  "岗位",
		"width":  "11",
		"is_num": "0",
	})
	dataKey = append(dataKey, map[string]string{
		"key":    "status",
		"title":  "权限",
		"width":  "12",
		"is_num": "0",
	})
	//填充数据
	data := make([]map[string]interface{}, 0)

	ex := tools.NewMyExcel()
	ex.ExportToWeb(dataKey, data, context)
}

func GetUserInfo(context *gin.Context) {
	/*参数用户*/
	userIdStr := context.Param("userId")
	//登录用户
	loginUserId, _ := context.Get("userId")
	userId := utils.GetInterfaceToInt(loginUserId)

	if userIdStr == "" {
		userIdStr = strconv.Itoa(userId)
	}
	useridP, _ := strconv.Atoi(userIdStr)

	service.SysUserService.CheckUserDataScope(userId, useridP)

	user := service.SysUserService.FindUserById(useridP)

	var roles []system.SysRoles
	var roleIds []int
	// 登录者的权限
	roles = service.SysRoleService.SelectRolePermissionByUserId(userId)

	posts := service.SysPostsService.SelectSysPostList(tools.SearchTableDataParam{
		Other: system.SysPost{},
	}, false).Rows

	var result = map[string]any{
		"user":  user,
		"roles": roles,
		"posts": posts,
	}
	// 判断当期是否为管理员
	if useridP != 0 {
		postIds := service.SysPostsService.SelectPostListByUserId(useridP)
		roles2 := service.SysRoleService.SelectRolePermissionByUserId(useridP)
		for _, sysRoles := range roles2 {
			roleIds = append(roleIds, sysRoles.RoleId)
		}
		result["postIds"] = postIds
		result["roleIds"] = roleIds
	}

	R.SuccessData(context, result)
}

func SaveUser(context *gin.Context) {
	userId, _ := context.Get("userId")
	var user system.SysUserParm
	if err := context.ShouldBind(&user); err != nil {
		panic("参数不能为空")
	}
	var username = user.UserName
	if service.SysUserService.IsExistUser(username) {
		panic("用户名已存在")
	}
	var phonenumber = user.Phonenumber
	if service.SysUserService.IsExistUserByPhoneNumber(phonenumber, 0) {
		panic("手机号已存在")
	}
	var email = user.Email
	if service.SysUserService.IsExistUserByEmail(email, 0) {
		panic("邮箱已存在")
	}
	var password = user.Password
	var pwd, _ = utils.PasswordHash(password)
	var user1 = service.SysUserService.FindUserById(userId)
	user.CreateBy = user1.UserName
	user.CreateTime = time.Now()
	user.Password = pwd

	/*用户名、手机号、邮箱不能重复验证*/
	var message = service.SysUserService.SaveUser(user)
	R.SuccessMsg(context, message)
}

func UploadUser(context *gin.Context) {
	userId, _ := context.Get("userId")
	var userParm system.SysUserParm
	if err := context.ShouldBind(&userParm); err != nil {
		panic("参数不能为空")
	}
	var uId = userParm.UserId
	if uId == 0 {
		panic("参数不能为空")
	}
	var user = service.SysUserService.FindUserById(userId)
	var userSql = service.SysUserService.FindUserById(uId)

	var username = userParm.UserName
	if !service.SysUserService.IsExistUser(username) {
		if userSql.UserName != username {
			panic("用户名:" + username + "已存在")
		}
	}
	var phonenumber = userParm.Phonenumber
	if service.SysUserService.IsExistUserByPhoneNumber(phonenumber, uId) {
		panic("手机号:" + phonenumber + "已存在")
	}
	var email = userParm.Email
	if service.SysUserService.IsExistUserByEmail(email, uId) {
		if userSql.Email != email {
			panic("邮箱:" + email + "已存在")
		}
	}
	userParm.UpdateBy = user.UserName
	userParm.UpdateTime = time.Now()
	service.SysUserService.UploadUser(userParm)
	R.Success(context)
}

func DeleteUserById(context *gin.Context) {
	var userIds = context.Param("userIds")
	service.SysUserService.DeleteUser(utils.Split(userIds))
	R.Success(context)
}

// 重设密码
func ResetPwd(context *gin.Context) {
	/*获取为空有可能内部参数错误*/
	var user system.SysUserParm
	if err := context.ShouldBind(&user); err != nil {
		panic("参数不能为空")
	}
	service.SysUserService.ResetPwd(user)
	R.Success(context)
}

func ChangeUserStatus(context *gin.Context) {
	var user system.SysUserParm
	if err := context.ShouldBind(&user); err != nil {
		panic("参数不能为空")
		return
	}
	if user.UserId == constants.AdminId {
		panic("管理员数据不开操作")
		return
	}
	service.SysUserService.ChangeUserStatus(user)
	R.Success(context)
}

func GetAuthUserRole(context *gin.Context) {
	userIdStr := context.Param("userId")
	userId, _ := strconv.Atoi(userIdStr)
	uIdStr, _ := context.Get("userId")
	uId := utils.GetInterfaceToInt(uIdStr)
	// 登录者的权限
	var roles []system.SysRoles
	if service.SysUserService.IsAdminById(uId) {
		roles = service.SysRoleService.SelectRolePermissionByUserId(uId)
	} else {
		roles = service.SysRoleService.SelectRolePermissionByUserId(userId)
	}

	user := service.SysUserService.FindUserById(userId)

	res := map[string]any{
		"user":  user,
		"roles": roles,
	}
	R.SuccessData(context, res)
}

func PutAuthUser(context *gin.Context) {
	uIdStr, _ := context.GetQuery("userId")
	uId, _ := strconv.Atoi(uIdStr)
	roleIds, _ := context.GetQuery("roleIds")
	uIds := []int{uId}
	service.SysRoleService.DeleteRolesByUser(uIds)
	result := service.SysRoleService.InsertRolesByUser(uId, utils.Split(roleIds))
	R.SuccessData(context, result)
}

// 登录获取菜单
func GetUserDeptTree(context *gin.Context) {
	var list = service.SysUserService.GetUserDeptTree()
	R.SuccessData(context, list)
}
